# Prepare NJ returns
# Note: clean and rearrange 2020 NJ returns
# By Dom Valentino and Chris Kenny
# libs --------------------------------------------------------------------
library(tidyverse) # select(), filter(), mutate(), etc.
library(haven) # read_dta()

# data --------------------------------------------------------------------
returns <- read.csv("../data/returns_nj/returns_nj.csv")[1:21, 1:8]
cvap <- read_dta("../data/modified data/county_cvap.dta") # read cvap data

# prep --------------------------------------------------------------------
final_nj <- returns %>% mutate(state = "NJ", year = 2020, treat = 1, fips = NA, dem_share_pres = dem_pres / (dem_pres + rep_pres),
                               dem_share_sen = dem_sen / (dem_sen + rep_sen), dem_share_gov = NA) %>% 
  left_join(., cvap, by = c("state", "county", "year")) %>% 
  mutate(turnout_share = ballots_cast / cvap_approx) %>% 
  select(year, state, county, fips, treat, dem_share_pres, dem_share_sen, dem_share_gov, ballots_cast, cvap, cvap_approx, cvap_moe, turnout_share)

# save
write_csv(final_nj, path = '../data/analysis/analysis_nj.csv')
